import axios from "axios";
export default {
  data() {
    return {
      loading3: false,
      loading4: false,
      current: 1,
      size: 10,
      total: 0,
      modal1: false,
      gallery: [],
      parentIdList: [],

      tableData2: [],
      imgurl1: "",
      imgurl2: [],
      tableColumn2: [
        {
          label: "展品名称",
          prop: "name"
        },
        {
          label: "展品编号",
          prop: "number"
        },
        {
          label: "所属展览",
          prop: "exhibitionName"
        },
        {
          label: "权重",
          prop: "weight"
        }
      ],
      name: '',
      exhibitionId: '',
      loading2:false,
      modal2: false,
      newData2: {
        about: "",
        gallery: [],
        name: "",
        exhibitionId: "",
        pic: "",
        weight: "",
        number: ""
      },
      exhibitionTypeList: [],
      id2: "",
      exhibitionId: "",
      modal3: false,
      ind: 0,
      clear: false,
      exhibitionList: [],
      loading7: false,
      ids: [],

      dialogAudio: false,
      audioData: [],

      addAudioDialog: false,
      audioForm: {
        name: "",
        audioLang: ""
      },
      musicId: "musicId",
      audioUrl: "",
      audioTitle: "新增音频",
      exhibitionExhibitId: "",
      loadingAudio: false,
      indx: 1
    };
  },
  created() {
    this.getExhibitionList();
    // this.getExhibitionTypeList()
  },
  methods: {
    show(data) {
      this.gallery = [];
      this.gallery = data;
      this.modal1 = true;
      console.log(data);
    },
    nameChange() {
      this.current = 1;
      this.getExhibitionHallList();
    },
    pidChange() {
      this.current = 1;
      this.getExhibitionHallList();
    },
    getExhibitionTypeList() {
      this.$http("/ztmuseum/stage/exhibition-type/selector", {}, "get", res => {
        console.log(res);
        if (res.data.code == 1000) {
          this.exhibitionTypeList = res.data.data;
        } else {
          this.$Message.error(res.data.msg);
        }
      });
    },
    allPush(){
      this.loading2=true
      if(this.ids.length==0){
        this.$Message.error('请勾选需要导出的展品')
        this.loading2=false
        return false
      }else{
        let that = this
      this.$confirm('此操作将批量导出展品二维码， 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showClose: false,
        type: 'warning'
      }).then(() => {
        axios({
          method: 'post',
          url: this.baseUrl + '/ztmuseum/stage/exhibition-exhibit/exportQrCode',
          headers: {
            'token': this.$store.state.token,
            'Content-Type': 'application/json;charset=UTF-8'
          },
          responseType: 'blob',
          data: {
            ids: this.ids
          }
        }).then(function (res) {
          console.log(res)
          that.loading2 = false
          let blob = new Blob([res.data], {
            type: 'application/x-zip-compressed'
          })
          let objectUrl = URL.createObjectURL(blob)
          let a = document.createElement('a')
          a.href = objectUrl
          a.download = '展品二维码'
          a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
          window.URL.revokeObjectURL(blob)
        }).catch(resp => {
          that.loading2 = false
          that.$Message.error('导出二维码失败')
        })
      }).catch(() => {
        this.loading2 = false
      })

      }

    },
    selectionChange(selection){
      this.ids=[]
      selection.map(item=>{
        this.ids.push(item.id)
      })
    },
    getExhibitionList () {
      this.$http('/ztmuseum/stage/exhibition/selector', {}, 'get', (res) => {
        console.log(res)
        if (res.data.code == 1000) {
          res.data.data.map(item => {
            if (item.id != "0") {
              this.exhibitionList.push(item);
            }
          });
          this.exhibitionId = this.exhibitionList[0].id;
          this.getExhibitionHallList();
        } else {
          this.$Message.error(res.data.msg);
        }
      });
    },
    changePage(data) {
      this.current = data;
      this.getExhibitionHallList();
    },
    handel(data) {
      console.log(data);
      this.newData2 = {
        about: data.about,
        exhibitionId: data.exhibitionId,
        gallery: data.gallery,
        name: data.name,
        number: data.number,
        pic: data.pic,
        weight: data.weight
      };
      this.id2 = data.id;
      this.imgurl1 = data.pic;
      this.imgurl2 = [];
      data.gallery.map(item => {
        this.imgurl2.push({
          name: item,
          percentage: 100,
          status: "finished",
          uid: item,
          url: item
        });
      });
      this.modal2 = true;
    },
    getExhibitionHallList() {
      this.loading3 = true;
      this.$http(
        "/ztmuseum/stage/exhibition-exhibit/getExhibitionExhibitList",
        {
          current: this.current,
          size: this.size,
          exhibitionId: this.exhibitionId,
          name: this.name
        },
        "post",
        res => {
          console.log(res);
          if (res.data.code == 1000) {
            this.tableData2 = res.data.data.records;
            this.total = res.data.data.total;
          } else {
            this.$Message.error(res.data.msg);
          }
          this.loading3 = false;
        }
      );
    },
    del2(data) {
      // console.log(data);
      this.$confirm("此操作将删除该展品， 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        type: "warning"
      })
        .then(() => {
          this.$http(
            "/ztmuseum/stage/exhibition-exhibit/deleteExhibitionExhibit",
            {
              id: data.id
            },
            "post",
            res => {
              if (res.data.code == 1000 && res.data.data) {
                this.$Message.success("删除成功");
                this.current = 1;
                this.getExhibitionHallList();
                // this.getBottomExhibitionHallDropDown()
              } else {
                this.$Message.error(res.data.msg);
              }
              this.modal3 = false;
            }
          );
        })
        .catch(() => {});
    },
    create() {
      this.ind++;
      this.newData2 = {
        about: "",
        exhibitionId: "",
        gallery: [],
        name: "",
        number: "",
        pic: "",
        weight: ""
      };
      this.modal2 = true;
      this.id2 = "";
      this.imgurl1 = "";
      this.clear = true;
      this.imgurl2 = [];
    },
    submit2() {
      this.$refs["ruleForm2"].validate(valid => {
        if (valid) {
          this.loading4 = true;
          console.log(this.newData2);
          if (this.id2 == "") {
            this.$http(
              "/ztmuseum/stage/exhibition-exhibit/addExhibitionExhibit",
              {
                about: this.newData2.about,
                name: this.newData2.name,
                weight: this.newData2.weight,
                exhibitionId: this.newData2.exhibitionId,
                number: this.newData2.number,
                gallery: this.newData2.gallery,
                pic: this.newData2.pic
              },
              "post",
              res => {
                if (res.data.code == 1000 && res.data.data) {
                  this.$Message.success("新增成功");
                  this.getExhibitionHallList();
                } else {
                  this.errorMessage(res.data.msg);
                }
                this.modal2 = false;
                this.loading4 = false;
              }
            );
          } else {
            this.$http(
              "/ztmuseum/stage/exhibition-exhibit/updateExhibitionExhibit",
              {
                about: this.newData2.about,
                name: this.newData2.name,
                weight: this.newData2.weight,
                exhibitionId: this.newData2.exhibitionId,
                number: this.newData2.number,
                gallery: this.newData2.gallery,
                pic: this.newData2.pic,
                id: this.id2
              },
              "post",
              res => {
                this.loading4 = false;
                if (res.data.code == 1000 && res.data.data) {
                  this.$Message.success("修改成功");
                  this.getExhibitionHallList();
                } else {
                  this.$Message.error(res.data.msg);
                }
                this.modal2 = false;
              }
            );
          }
        }
      });
    },
    catchData2(data) {
      this.newData2.about = data;
    },
    uploadImg1(data) {
      console.log(data);
      this.newData2.pic = data;
      console.log(this.imgurl1);
      this.imgurl1 = data;
    },
    uploadImg2(data) {
      this.newData2.gallery = [];
      this.imgurl2 = [];
      data.map(item => {
        this.newData2.gallery.push(item.url);
      });

      this.imgurl2 = data;
      console.log(data, 1111);
    },
    download(data) {
      this.ids = [data.id];
      let that = this;
      this.loading7 = true;
      this.$confirm("此操作将导出该展品二维码， 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        type: 'warning'
      }).then(() => {
        axios({
          method: 'post',
          url: this.baseUrl + '/ztmuseum/stage/exhibition-exhibit/exportQrCode',
          headers: {
            'token': this.$store.state.token,
            'Content-Type': 'application/json;charset=UTF-8'
          },
          responseType: 'blob',
          data: {
            ids: this.ids
          }
        }).then(function (res) {
          console.log(res)
          that.loading7 = false
          let blob = new Blob([res.data], {
            type: 'application/x-zip-compressed'
          })
          let objectUrl = URL.createObjectURL(blob)
          let a = document.createElement('a')
          a.href = objectUrl
          a.download = '展品二维码'
          a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
          window.URL.revokeObjectURL(blob)
          this.ids=[]
        }).catch(resp => {
          that.loading7 = false
          that.$Message.error('导出二维码失败')
          this.ids=[]
        })
      }).catch(() => {
        this.loading7 = false
        this.ids=[]
      })
        .then(() => {
          axios({
            method: "post",
            url:
              this.baseUrl + "/ztmuseum/stage/exhibition-exhibit/exportQrCode",
            headers: {
              token: this.$store.state.token,
              "Content-Type": "application/json;charset=UTF-8"
            },
            responseType: "blob",
            data: {
              ids: this.ids
            }
          })
            .then(function(res) {
              console.log(res);
              that.loading7 = false;
              let blob = new Blob([res.data], {
                type: "application/x-zip-compressed"
              });
              let objectUrl = URL.createObjectURL(blob);
              let a = document.createElement("a");
              a.href = objectUrl;
              a.download = "展览二维码";
              a.dispatchEvent(
                new MouseEvent("click", {
                  bubbles: true,
                  cancelable: true,
                  view: window
                })
              );
              window.URL.revokeObjectURL(blob);
            })
            .catch(resp => {
              that.loading7 = false;
              that.errorMessage("导出二维码失败");
            });
        })
        .catch(() => {
          this.loading7 = false;
        });

      // this.modal4 = true
      // this.bottomWords = '微信扫一扫,听语音讲解'
    },

    openA(row) {
      console.log(row);
      this.exhibitionExhibitId = row.id;
      this.getAudioList();
      this.dialogAudio = true;
    },
    getAudioList() {
      this.loadingAudio = true;
      this.$http(
        "/ztmuseum/stage/exhibition-exhibit-audio/getExhibitAudio",
        {
          exhibitId: this.exhibitionExhibitId
        },
        "get",
        res => {
          if (res.data.code == 1000 && res.data.data) {
            this.loadingAudio = false;
            this.audioData = res.data.data;
          } else {
            this.$Message.error(res.data.msg);
          }
          // this.modal2 = false;
        }
      );
    },
    handleClose() {
      this.dialogAudio = false;
    },

    // 产看音频
    showAudio(row) {},
    // 打开音频弹窗
    addAudio() {
      this.audioId = "";
      this.audioForm = {
        name: "",
        audioLang: ""
      };
      this.audioDuration = "";
      this.audioUrl = "";
      this.indx++;
      this.musicId = "musicId" + this.indx;
      this.addAudioDialog = true;
      this.audioTitle = "新增音频";
    },
    cancelAudio() {
      this.addAudioDialog = false;
      this.audioId = "";
      this.audioForm = {
        name: "",
        audioLang: ""
      };
      this.audioDuration = "";
      this.audioUrl = "";
    },
    uploadAudio(url) {
      console.log(url);
      this.audioUrl = url;
    },
    audioLength(duration) {
      console.log(duration);
      this.audioDuration = duration;
    },
    updateAudio(row) {
      console.log(row);
      this.audioId = row.id;
      this.audioForm.name = row.name;
      this.audioForm.audioLang = row.audioLang;
      this.audioDuration = row.audioDuration;
      this.audioUrl = row.audioUrl;
      console.log(this.audioUrl);
      this.addAudioDialog = true;
    },
    subAudio() {
      if (this.audioTitle == "新增音频") {
        this.$http(
          "/ztmuseum/stage/exhibition-exhibit-audio/addExhibitAudio",
          {
            audioDuration: this.audioDuration,
            audioLang: this.audioForm.audioLang,
            audioUrl: this.audioUrl,
            exhibitionExhibitId: this.exhibitionExhibitId,
            name: this.audioForm.name
          },
          "post",
          res => {
            if (res.data.code == 1000 && res.data.data) {
              this.$Message.success(res.data.msg);

              this.getAudioList();
              this.cancelAudio();
            } else {
              this.$Message.error(res.data.msg);
            }
          }
        );
      } else {
        this.$http(
          "/ztmuseum/stage/exhibition-exhibit-audio/updateExhibitAudio",
          {
            audioDuration: this.audioDuration,
            audioLang: this.audioForm.audioLang,
            audioUrl: this.audioUrl,
            id: this.audioId,
            name: this.audioForm.name
          },
          "post",
          res => {
            if (res.data.code == 1000 && res.data.data) {
              this.$Message.success(res.data.msg);

              this.getAudioList();
              this.cancelAudio();
            } else {
              this.$Message.error(res.data.msg);
            }
          }
        );
      }
    },

    del3(row) {
      this.$confirm("此操作将删除该音频， 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        type: "warning"
      })
        .then(() => {
          this.$http(
            "/ztmuseum/stage/exhibition-exhibit-audio/deleteExhibitAudio",
            {
              id: row.id
            },
            "post",
            res => {
              if (res.data.code == 1000 && res.data.data) {
                this.$Message.success("删除成功");
                this.getAudioList();
              } else {
                this.$Message.error(res.data.msg);
              }
              this.modal3 = false;
            }
          );
        })
        .catch(() => {});
    }
  }
};
